package com.example.eldersystem.controller;

import com.example.eldersystem.operatedata.model.SystemUser;
import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.service.SystemUserService;
import com.example.eldersystem.operatedata.model.Detector;
import com.example.eldersystem.operatedata.service.DetectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhenzi.sms.ZhenziSmsClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/")
public class MainController {

    @Autowired
    SystemUserService systemUserService;
    @Autowired
    DetectorService detectorService;

    @RequestMapping("login")
    private DataResponse<SystemUser> login(@RequestBody Map<String,Object> map){
        DataResponse<SystemUser> dataResponse = new DataResponse<>();
        String userName = map.get("userName").toString();
        String password = map.get("password").toString();
        dataResponse = systemUserService.login(userName,password);
        return  dataResponse;
    }

    @RequestMapping("changePhoto")
    private DataResponse<String> updateLogoImage(@RequestBody Map<String,Object> map){
        DataResponse<String> dataResponse = new DataResponse<>();
        dataResponse.setCode(6);
        int id=Integer.parseInt(map.get("id").toString());
        String logoimage=map.get("logoimage").toString();
        dataResponse = systemUserService.updateLogoImage(id,logoimage);
        return dataResponse;
    }

    @RequestMapping("addCamera")
    private DataResponse<String> addCamera(@RequestBody Map<String,Object> map){

        DataResponse<String> dataResponse = new DataResponse<>();
        Detector detector = new Detector();
        detector.setIp(map.get("ip").toString());
        detector.setPort(map.get("port").toString());
        detector.setType(map.get("type").toString());
        detector.setName(map.get("name").toString());
        dataResponse = detectorService.addDetector(detector);
        return dataResponse;
    }

    @RequestMapping("getCamera")
    private DataResponse<List<Detector>> selectDetector(){
        DataResponse<List<Detector>> dataResponse;
        dataResponse=detectorService.selectDetector();
        return dataResponse;
    }

    @PostMapping("/send")
    public DataResponse<String> sendMSM(@RequestParam String telephone) throws Exception {
        String apiUrl = "http://sms_developer.zhenzikj.com";
        String appId = "107456";
        String appSecret = "bb3f909f-ef88-4343-8a19-9930e728a0f8";

        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl,appId, appSecret);

        HashMap<String, Object> map = new HashMap<>();
        //这个是榛子云短信平台用户中心下的短信管理的短信模板的模板id
        map.put("templateId", "2705");
        //生成验证码
        int pow = (int) Math.pow(10, 4 - 1);
        String verificationCode = String.valueOf((int) (Math.random() * 9 * pow + pow));
        //随机生成messageId，验证验证码的时候，需要携带这个参数去取验证码
        String messageId = UUID.randomUUID().toString();
        map.put("messageId", messageId);
        String[] templateParams = new String[2];
        //两个参数分别为验证码和过期时间
        templateParams[0] = verificationCode;
//        templateParams[0] = "5211314";
        templateParams[1] = String.valueOf("五分钟");
        map.put("templateParams", templateParams);
        map.put("number", telephone);

        String result = client.send(map);

        DataResponse<String> dataResponse = new DataResponse<>();
        dataResponse.setData(verificationCode);
        return dataResponse;

        //return Result.success(verificationCode);
    }
}
